<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- 底部按钮 -->
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getProfile } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    employeeId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [],
      rolesList: []
    }
  },
  created() {
    this.getRoles()
    this.getProfile()
  },

  methods: {
    // 分配角色
    async submit() {
      const res = await assignRoles({ id: this.employeeId, roleIds: this.checkList })
      // console.log(res)
      // 提示
      this.$message.success(res.message)
      // 关闭弹框
      this.$emit('close')
    },

    // 通过id获取员工信息 进行角色回填
    async getProfile() {
      const res = await getProfile(this.employeeId)
      // console.log(res)
      this.checkList = res.data.roleIds === null ? [] : res.data.roleIds
    },

    // 获取角色列表
    async getRoles() {
      const res = await getRoles()
      // console.log(res)
      this.rolesList = res.data.rows
    },

    // 取消弹框
    cancleDialog() {
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
